package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pojo.PatientDoctor;
import service.PatientDoctorService;

import java.util.List;

@Api(value = "患者医生关联管理", description = "患者与医生关联关系相关接口")
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/patientDoctor")
public class PatientDoctorController {

    @Autowired
    private PatientDoctorService patientDoctorService;

    @ApiOperation(value = "查询所有患者医生关联记录", notes = "获取所有患者与医生的关联信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回关联记录列表", response = PatientDoctor.class, responseContainer = "List")
    })
    @GetMapping("/findAll")
    public ResponseEntity<List<PatientDoctor>> findAll() {
        return ResponseEntity.ok(patientDoctorService.findAll());
    }

    @ApiOperation(value = "根据ID查询关联记录", notes = "通过ID获取单个患者医生关联信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回关联记录", response = PatientDoctor.class),
            @ApiResponse(code = 404, message = "未找到对应记录")
    })
    @GetMapping("/findById/{id}")
    public ResponseEntity<PatientDoctor> findById(@PathVariable Integer id) {
        PatientDoctor record = patientDoctorService.findById(id);
        return record != null ? ResponseEntity.ok(record) : ResponseEntity.notFound().build();
    }

    @ApiOperation(value = "新增患者医生关联记录", notes = "创建一条新的患者与医生关联记录")
    @ApiResponses({
            @ApiResponse(code = 201, message = "关联记录创建成功"),
            @ApiResponse(code = 400, message = "无效的关联记录数据")
    })

    @PostMapping("/addPatientDoctor")
    public ResponseEntity<Void> addPatientDoctor(@RequestBody PatientDoctor record) {
        // 新增校验：同一天同一医生同一病人只能挂一次号
        int count = patientDoctorService.countByPatientDocDay(record.getPatientId(), record.getDocId(), record.getDay());
        if (count > 0) {
            // 已挂号，返回409冲突
            return ResponseEntity.status(HttpStatus.CONFLICT).build();
        }
        patientDoctorService.addPatientDoctor(record);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @ApiOperation(value = "更新关联记录信息", notes = "根据ID更新患者医生关联记录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "关联记录更新成功"),
            @ApiResponse(code = 404, message = "未找到对应记录")
    })
    @PostMapping("/updatePatientDoctor")
    public ResponseEntity<Void> updatePatientDoctor(@RequestBody PatientDoctor record) {
        int success = patientDoctorService.updatePatientDoctor(record);
        return success > 0 ?
                ResponseEntity.ok().build() :
                ResponseEntity.notFound().build();
    }

    @ApiOperation(value = "删除关联记录", notes = "根据ID删除患者医生关联记录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "关联记录删除成功"),
            @ApiResponse(code = 404, message = "未找到对应记录")
    })
    @DeleteMapping("/deletePatientDoctor/{id}")
    public ResponseEntity<Void> deletePatientDoctor(@PathVariable Integer id) {
        int success = patientDoctorService.deletePatientDoctor(id);
        return success > 0 ?
                ResponseEntity.ok().build() :
                ResponseEntity.notFound().build();
    }

    @GetMapping("/findByDocId")
    @ApiOperation(value = "根据医生ID查询患者", notes = "通过医生ID获取患者列表")
    @ApiResponses({
        @ApiResponse(code = 200, message = "成功返回患者列表", response = PatientDoctor.class, responseContainer = "List")
    })
    public ResponseEntity<List<PatientDoctor>> findByDocId(@RequestParam String docId) {
        List<PatientDoctor> list = patientDoctorService.findByDocId(docId);
        return ResponseEntity.ok(list);
    }

    @PostMapping("/markAsDiagnosed")
    @ApiOperation(value = "将患者诊断状态标记为已诊断", notes = "根据ID将诊断状态设为1")
    @ApiResponses({
        @ApiResponse(code = 200, message = "标记成功"),
        @ApiResponse(code = 404, message = "未找到对应记录")
    })
    public ResponseEntity<Void> markAsDiagnosed(@RequestParam int id) {
        int result = patientDoctorService.markAsDiagnosed(id);
        return result > 0 ? ResponseEntity.ok().build() : ResponseEntity.notFound().build();
    }
}